<template>
  <el-card class="spu-card">
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="showSaveSpu"
      >添加Spu</el-button
    >

    <el-table :data="spuList" border class="spu-table">
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column prop="spuName" label="Spu名称" />
      <el-table-column prop="description" label="Spu描述" />
      <el-table-column label="操作" width="240">
        <template #default="{ row }">
          <!-- <template v-slot="scope"> -->
          <el-tooltip content="添加Sku" placement="top" effect="light">
            <el-button
              size="small"
              type="primary"
              :icon="Plus"
              @click="showSaveSku(row)"
            />
          </el-tooltip>

          <el-tooltip content="修改Spu" placement="top" effect="light">
            <el-button
              size="small"
              type="warning"
              :icon="Edit"
              @click="showUpdateSpu(row)"
            />
          </el-tooltip>

          <el-tooltip content="查看Sku列表" placement="top" effect="light">
            <el-button size="small" type="info" :icon="InfoFilled" />
          </el-tooltip>

          <el-tooltip content="删除Spu" placement="top" effect="light">
            <el-button size="small" type="danger" :icon="Delete" />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      layout="prev, pager, next, jumper, total, sizes"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
</template>

<script lang="ts">
export default {
  name: "SpuList",
};
</script>

<script lang="ts" setup>
import { ref, watch } from "vue";
import { Plus, Edit, Delete, InfoFilled } from "@element-plus/icons-vue";
import { useCategoryStore } from "@/stores/category";
import { getSpuListApi } from "@/api/product/spu";
import type { SpuList, SpuItem } from "@/api/product/model/spuModel";

const emit = defineEmits(["setIsShow", "setSpu"]);

// 显示添加Sku
const showSaveSku = (row: SpuItem) => {
  emit("setIsShow", 3);
  emit("setSpu", row);
};
// 显示添加Spu
const showSaveSpu = () => {
  emit("setIsShow", 2);
};
// 显示修改Spu
const showUpdateSpu = (row: SpuItem) => {
  emit("setIsShow", 2);
};

const spuList = ref<SpuList>([]);
const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);

const categoryStore = useCategoryStore();

// 发送请求的方法
const getSpuList = async () => {
  const res = await getSpuListApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id: categoryStore.category3Id as number,
  });
  total.value = res.total;
  spuList.value = res.records;
};

const handleSizeChange = (newPageSize: number) => {
  pageSize.value = newPageSize;
  getSpuList();
};
const handleCurrentChange = (newCurrentPage: number) => {
  currentPage.value = newCurrentPage;
  getSpuList();
};

watch(
  // categoryStore // 监视整个对象
  () => categoryStore.category3Id, // 如果想监视某个属性，需要写成函数形式
  (newVal) => {
    if (newVal) {
      getSpuList();
      return;
    }
    // 没有三级分类id，清空数据
    spuList.value = [];
    currentPage.value = 1;
    pageSize.value = 3;
    total.value = 0;
  }
);
</script>

<style scoped>
.spu-card {
  margin-top: 20px;
}

.spu-table {
  margin: 10px 0;
}
</style>
